<?php
defined('IN_ZEEGING') or die('Access Denied');
class Controller extends Base{
	protected $param=array();
	protected $view=NULL;
	public function __construct(){
		$this->view = new View();
		$this->getParam();
	}
	public function run(){
		$controlPath=ZEEGING_PATH.'Controllers'.DIRECTORY_SEPARATOR.$this->param['control'].'Controller.class.php';
		if(file_exists($controlPath)){
			include $controlPath;
			$className=ucwords($this->param['control']).'Controller';
			if(class_exists($className)){
				$instance=new $className;
				$methodName=$this->param['action'].'Action';
				if(method_exists($instance,$methodName)){
					$instance->$methodName();
				}
				else{
					exit('Method '.$methodName.' does not exist.');
				}
			}
			else{
				exit('Class does not exist.');
			}
		}
		else{
			exit('Controller '.$controlPath.' does not exist.');
		}
	}
	protected function getParam(){
		$base_configs=$this->loadConfigs('base');
		if($base_configs['URL_MODE']==1){
			$c = isset($_GET['c']) && !empty($_GET['c']) ? $_GET['c'] : (isset($_POST['c']) && !empty($_POST['c']) ? $_POST['c'] : '');
			$c = $this->safe_deal($c);
			$a = isset($_GET['a']) && !empty($_GET['a']) ? $_GET['a'] : (isset($_POST['a']) && !empty($_POST['a']) ? $_POST['a'] : '');
			$a = $this->safe_deal($a);
		}
		else if ($base_configs['URL_MODE']==2){
			if(isset($_SERVER['PATH_INFO'])){
				$path=trim($_SERVER['PATH_INFO'],'/');
				$paths=explode('/',$path);
				$c=array_shift($paths);
				$a=array_shift($paths);
				while(($getKey=array_shift($paths))&&($getVal=array_shift($paths))){
					$_GET[$getKey]=$getVal;
					$_REQUEST[$getKey]=$getVal;
				}
			}
		}
		else{
			//base configs error
		}
		if (empty($c)) {
			$this->param['control'] = Base::is_cli() ? $base_configs['CLI_DEFAULT_CONTROL'] : $base_configs['DEFAULT_CONTROL'];
		} else {
			if(is_string($c)) $this->param['control'] = ucwords($c);
		}
		if (empty($a)) {
			$this->param['action'] = Base::is_cli() ? $base_configs['CLI_DEFAULT_ACTION'] : $base_configs['DEFAULT_ACTION'];
		} else {
			if(is_string($a)) $this->param['action'] = $a;
		}
	}
	
	public function assign($var,$value){
		$this->view->assign($var,$value);
	}
	public function display($action='',$control=''){
		$action = empty($action) ? $this->param['action'] : $action ;
		$control = empty($control) ? $this->param['control'] : $control ;
		
		$file = $control . '_' . $action ;
		$this->view->display($file);
	}
	
	protected function loadModel($file=NULL){
		$file = empty($file) ? $this->param['control'] : $file;
		$file=ucwords($file);
		$modelPath=ZEEGING_PATH.'Models'.DIRECTORY_SEPARATOR.$file.'Model.class.php';
		if(file_exists($modelPath)){
			$className=$file.'Model';
			if(!class_exists($className)){
				include_once $modelPath;
			}
			if(class_exists($className)){
				return new $className();
			}
			else{
				exit('Class does not exist.');
			}
		}
		else{
			exit('Model does not exist.');
		}
	}
	
	protected function GET($key=NULL){
		if(isset($key)){
			return isset($_GET[$key]) ? $_GET[$key] : NULL;
		}
		else{
			return $_GET;
		}
	}
	protected function POST($key=NULL){
		if(isset($key)){
			return isset($_POST[$key]) ? $_POST[$key] : NULL;
		}
		else{
			return $_POST;
		}
	}
	protected function REQUEST($key){
		return isset($_REQUEST[$key]) ? $_REQUEST[$key] : NULL;
	}
	
	/**
	 * 安全处理函数
	 * 处理m,a,c
	 */
	private function safe_deal($str) {
		return str_replace(array('/', '.'), '', $str);
	}
}

//------------------------------------------------
//End of File : /Core/Controller.class.php
//------------------------------------------------